package com.gray.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gray.common.UploadImage;
import com.gray.dto.ArticleDto;
import com.gray.pojo.Article;
import com.gray.pojo.ArticleSort;
import com.gray.pojo.User;
import com.gray.service.impl.ArticleServiceImpl;

@Controller
@RequestMapping(value = "/article")
public class ArticleController {

	@Autowired
	private ArticleServiceImpl articleServiceImpl;
	

	/**
	 * 
	 * @param request
	 * @param currentPage
	 *            当前页
	 * @param everyPageNum
	 *            每一页需要展示的数量
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getarticlebyuserandpage")
	public Map getArticleByUserAndPage(HttpServletRequest request, @RequestParam Integer currentPage,
			@RequestParam Integer everyPageNum) {
		User user = (User) request.getSession().getAttribute("user");
		Integer userId = null;
		Map map = null;
		if (user != null) {
			userId = user.getUserId();
			map = articleServiceImpl.getArticleByUserAndPage(userId, currentPage, everyPageNum);
		}

		return map;
	}

	/**
	 * 根据 用户 删除单个文章
	 * 
	 * @param request
	 * @param articleId
	 * @return 删除记录数
	 */

	@ResponseBody
	@RequestMapping(value = "deletesimplearticle")
	public int deleteSimpleArticle(HttpServletRequest request, @RequestParam int articleId) {
		User user = (User) request.getSession().getAttribute("user");

		Integer userId = null;
		if (user != null) {
			userId = user.getUserId();
			return articleServiceImpl.delArticleByUserAndArticleId((short) articleId, userId);
		}
		return 0;
	}

	/**
	 * 搜索文章
	 * 
	 * @param articleName
	 * @return
	 */

	@ResponseBody
	@RequestMapping(value = "/getsearcharticlebyarticlename")
	public Map getSearchArticleByArticleName(
			@RequestParam(value = "articleName") String articleName,
			@RequestParam(value="currentPage") Integer currentPage,
			@RequestParam(value="onePageNum") Integer onePageNum) {
		Map map = null;
		System.out.println(articleName);
		map = articleServiceImpl.getArticleByLikeArticleName(articleName, currentPage, onePageNum);
		return map;
	}

	/**
	 * 首页 处点击跳转到 精彩文章
	 * 
	 * @return
	 */
	@RequestMapping(value = "wonderfularticle")
	public String wonderfulArticle() {
		return "wonderfularticle";
	}

	/**
	 * 获取点击量排行高的文章（精彩文章）
	 * 
	 * @param currentPage
	 *            当前页
	 * @param onePageNum
	 *            每一页应该显示的数目
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getwonderfularticle")
	public Map getWonderfulArticle(@RequestParam(value = "currentPage", required = true) Integer currentPage,
			@RequestParam(value = "onePageNum", required = true) Integer onePageNum) {
		Map map = articleServiceImpl.getArticleByArticleClick(currentPage, onePageNum);

		return map;
	}

	/**
	 * 获取单篇文章 sesscion 获取articleId
	 * 
	 * @param articleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getonearticle")
	public Map getOneArticle(HttpServletRequest request) {
		int articleId = (int) request.getSession().getAttribute("articleId");
		Map map = new HashMap();
		List list = articleServiceImpl.getArticleByArticleId((short) articleId);
		map.put("article", list);
		return map;
	}
	/**
	 * 根据用户 和 文章id获取单个文章
	 * @param request
	 * @param articleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getsimplearticle")
	public Map getSimpleArticle(HttpServletRequest request, @RequestParam(required = true) int articleId) {
		Map map = new HashMap();
		User user = (User) request.getSession().getAttribute("user");
		ArticleDto article = null;
		if (user != null) {
			article = articleServiceImpl.getSimpleArticleDtoByUserAndArticleId(user.getUserId(), (short) articleId);
		}
		if (article != null) {
			map.put("article", article);
		} else {
			map.put("false", "没有该文章");
		}
		return map;
	}

	/**
	 * 跳转到文章显示页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "showarticle")
	public String article(HttpServletRequest request, @RequestParam(required = true) int articleId) {
		request.getSession().setAttribute("articleId", articleId);

		return "article";
	}

	/**
	 * 获取批量文章 信息 表格 （管理页面）
	 * 
	 * @param request
	 * @param pageSize
	 * @param currentPage
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getArticledtobyuserandpage")
	public Map getArticleDtoByUserAndPage(HttpServletRequest request, @RequestParam(required = true) int pageSize,
			@RequestParam(required = true) int currentPage) {
		User user = (User) request.getSession().getAttribute("user");
		Map map = null;
		if (user == null) {
			return null;
		}
		map = articleServiceImpl.getArticleDtoByUserAndPage(user.getUserId(), currentPage, pageSize);

		return map;
	}

	/**
	 * 删除文章 失败返回 0 ；
	 * 
	 * @param request
	 * @param userId
	 * @param articleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "delArticlebyuserandarticleid")
	public int delArticleByUserAndArticleId(HttpServletRequest request, @RequestParam(required = true) int articleId) {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return 0;
		}
		return articleServiceImpl.delArticleByUserAndArticleId(user.getUserId(), (short) articleId);
	}
	
	/**
	 * 更新文章
	 * @param request
	 * @param articleId
	 * @param sortArticleId
	 * @param articleName
	 * @param articleType
	 * @param articleUp
	 * @param articleSupport
	 * @return
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping(value = "updatearticleinfo")
	public int updateArticleInfo(HttpServletRequest request,
			@RequestParam(required = true)Integer articleId,
			@RequestParam(required = false)Integer sortArticleId,
			@RequestParam(required = false)String articleName,
			@RequestParam(required = false)Integer articleType,
			@RequestParam(required = false)Integer articleUp,
			@RequestParam(required = false)Integer articleSupport,
			@RequestParam(required = false)Integer sectionId,
			@RequestParam(required = false)MultipartFile file) throws IOException{
		User user = (User) request.getSession().getAttribute("user");
		
		
		//TODO 验证版块  sectionId不存在的情况
		
		
		Article article = new Article();
		if(sectionId!=null){
			article.setTypeId(sectionId.byteValue());
		}
		article.setUserId(user.getUserId());
		article.setArticleId(articleId.shortValue());
		if(sortArticleId!=null)
		article.setSortArticleId(sortArticleId);
		if(articleName!=null)
		article.setArticleName(articleName);
		if(articleType!=null)
		article.setArticleType(articleType);
		if(articleUp!=null)
		article.setArticleUp(articleUp.byteValue());
		if(articleSupport!=null)
		article.setArticleSupport(articleSupport.byteValue());
		article.setArticleTime(new Date());
		UploadImage uploadImage  = null;
		if(file!=null){
		 uploadImage = new UploadImage();
		try {
			uploadImage.setInputStream(file.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		uploadImage.setOriginalFilename(file.getOriginalFilename());
		return articleServiceImpl.updateArticle(article,uploadImage);
		}
		 uploadImage = new UploadImage();
		 uploadImage.setOriginalFilename("我不是图片.iii");
		return articleServiceImpl.updateArticle(article,uploadImage);
	}
	
	
	/**
	 * 获取该板块的所有文章
	 * @param sectionId
	 * @param currentPage
	 * @param onePageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getarticlebysectionId")
	public Map getArticleBySectionId(
			@RequestParam(value="sectionId") byte sectionId,
			@RequestParam(value="currentPage") Integer currentPage,
			@RequestParam(value="onePageNum") Integer onePageNum){
		Map map = articleServiceImpl.getArticleBySectionId(sectionId, currentPage, onePageNum);
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="getcarousel")
	public Map getCarousel(){
		Map map = new HashMap();
		List list = articleServiceImpl.selcectCarousel();
		map.put("article", list);
		return map;
	}
	
	/**
	 * 更新用户分组  
	 * @param req
	 * @param sortArticleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="updatearticlesort")
	public Map updateArticleSort(HttpServletRequest req,
			@RequestParam(value="sortArticleId",required=true)int sortArticleId,
			@RequestParam(value="sortArticleName",required=true)String sortArticleName){
		User user =(User) req.getSession().getAttribute("user");
		if(user != null){
			Map map = new HashMap();
			ArticleSort articleSort = new ArticleSort();
			articleSort.setUserId(user.getUserId());
			articleSort.setSortArticleId(sortArticleId);
			articleSort.setSort_article_name(sortArticleName);
			int i = articleServiceImpl.updateArticleSort(articleSort);
			System.out.println(i);
			if(i>0)
			map.put("sucess", true);
			return map;
		}else{
			return null;
		}
	}

}
